<template>
  <div>
    <div class="div">
      <div class="box"></div>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="用户名" prop="username">
    <el-input v-model="ruleForm.username"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input v-model="ruleForm.password"></el-input>
  </el-form-item>
  <el-form-item>


    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
    </div>
  </div>
</template>

<script>
import { getLogin ,getMenus} from "../api/api"
  export default {
    data() {
      return {
        ruleForm: {
          password: '',
          username:""
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        
        this.$refs[formName].validate(async valid => {
          if (valid) {
            // alert('submit!');
            let res= await getLogin(this.ruleForm)
            console.log(res,"res")
            if(res.meta.status==200){
                console.log('登陆成功',res.data)
                localStorage.setItem("token",res.data.token)
               
                this.$router.push("/")
            }

            // 异步存储 token
          //   this.$store.dispatch('setLogin',this.ruleForm).then(res=>{
          //          if(res.meta.status===200){
          //              location.href='/#/'
          //       this.$message.success("登陆成功")
          //        }
          //   })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    //   async getSide() {
    //   let res = await getMenus();
    //   console.log(res, "res");
    //   localStorage.setItem("menuList",JSON.stringify(res.data))
    // },
    }
  }
</script>

<style scoped>
.div {
  width: 480px;
  height: 330px;
  /* border: 1px solid #333; */
  margin: 120px auto;
  position: relative;
  padding-top: 50px;
  /* background-color: darkorange; */
}
.div .box {
  width: 120px;
  height: 120px;
  border: 3px solid #ddd;
  border-radius: 50%;
  position: absolute;
  top: -25%;
  left: 35%;
  /* text-align: center;
    line-height: 120px; */
  background-image: url("../assets/logo.png");
  background-repeat: no-repeat;
  background-size: 97px 103px;
  background-position: 53% 112%;
  box-shadow: 0px 5px 5px 5px #ddd;
}
</style>